package newPan3d.content.npc
{
	import _Pan3D.base.Camera3D;
	import _Pan3D.base.Object3D;
	import _Pan3D.core.Calculation;
	import _Pan3D.core.Groundposition;
	import _Pan3D.core.TestTriangle;
	import _Pan3D.role.Npc;
	
	import _me.Scene_data;
	
	import flash.geom.Point;
	import flash.geom.Vector3D;

	public class NpcMenager
	{
		private static var _thisCam:Camera3D;
		private static var _baseArr:Vector.<Object3D>;
		private static var _pointArr:Vector.<Object3D>;

		private static var _triangleClass:TestTriangle=new TestTriangle(); //引用一个测试类，
		public function NpcMenager()
		{
		}
		
		
		
		public static function findClikNpc(_mousex:int,_mousey:int):Npc
		{
			_thisCam= Scene_data.cam3D;
			
			_baseArr=new Vector.<Object3D>;
			_baseArr.push(new Object3D(-20,20,0));
			_baseArr.push(new Object3D(-20,0,0));
			_baseArr.push(new Object3D(20,0,0));
			
			_baseArr.push(new Object3D(-20,20,0));
			_baseArr.push(new Object3D(20,0,0));
			_baseArr.push(new Object3D(20,20,0));
			
			
			_pointArr=new Vector.<Object3D>;
			_pointArr.push(new Object3D(0,0,0));
			_pointArr.push(new Object3D(0,0,0));
			_pointArr.push(new Object3D(0,0,0));
			_pointArr.push(new Object3D(0,0,0));
			
			_pointArr.push(new Object3D(0,0,0));
			_pointArr.push(new Object3D(0,0,0));
			_pointArr.push(new Object3D(0,0,0));
			_pointArr.push(new Object3D(0,0,0));
			
			
			
			var f:Number=(Scene_data.stage.mouseX - Scene_data.stage3D.x)- _thisCam.fovw / 2;
			var g:Number= _thisCam.fovh / 2-(Scene_data.stage.mouseY - Scene_data.stage3D.y);

			
			/*
			var pos:Vector3D=new Vector3D(0,0,0);
			if (NpcLevel.npcItem)
			{
				var npc:Npc=NpcLevel.npcItem[0];
				if (npc)
				{
					pos=new Vector3D(npc.x,0,npc.z)
				}
			}
			*/
			
			for each(var npc:Npc in NpcLevel.npcItem){
				if(npc&&npc.inRender){
					var pos:Vector3D=new Vector3D(npc.x,0,npc.z);
					if(  TestSampleNpc(pos,f,g)){
						//trace("不得了了，找到了哦",npc.id);
						npc.stopWalk();
						npc.selected = true;
						return npc;
					}
				}
			}
			return null ;
			
			 
		}
		
		private static function TestSampleNpc(pos:Vector3D,f:Number,g:Number):Boolean
		{
			for(var i:int=0;i<_baseArr.length;i++)
			{
				_pointArr[i].x=_baseArr[i].x+pos.x;
				_pointArr[i].y=_baseArr[i].y+pos.y;
				_pointArr[i].z=_baseArr[i].z+pos.z;
				Groundposition.math_change_point(_pointArr[i]);
			}
			
			var TT0:Boolean=TestPointHipTriangle(_pointArr[0],_pointArr[1],_pointArr[2], f,g);
			var TT1:Boolean=TestPointHipTriangle(_pointArr[3],_pointArr[4],_pointArr[5], f,g);
			return TT0||TT1;
			if(TT0||TT1)
			{
				trace("确定找到了一个哦");
			}
		}
		
		public static function TestPointHipTriangle(A:Object3D,B:Object3D,C:Object3D,g:Number,f:Number):Boolean
		{
			if((A.rx==B.rx&&B.rx==C.rx)||(A.ry==B.ry&&B.ry==C.ry)||A.rz<10||B.rz<10||C.rz<10){
				return false;
			}
			var P:Object3D= Calculation._get_hit_rec(A, B, C, _thisCam, g,  f);
			//var T:TestTriangle = new TestTriangle( new Point(A.rx,A.ry), new Point(B.rx,B.ry), new Point(C.rx,C.ry));
			_triangleClass.setAllPoint( new Point(A.rx,A.ry), new Point(B.rx,B.ry), new Point(C.rx,C.ry))
			var k:Boolean= _triangleClass.checkPointIn(new Point(P.rx,P.ry))
				
		
				/*
			trace(A.rx,A.ry,A.rz);
			trace(B.rx,B.ry,B.rz);
			trace(C.rx,C.ry,C.rz);
			trace("=>",int(P.rx),int(P.ry),int(P.rz));
			trace(k);
			*/
			
			return k;
		
		}

		
		public static function math_change_point(_3dpoint : Object3D) : void {
			//对坐标系里的原始点，跟据镜头角度算出新的从坐标
			var rx : Number = _3dpoint.x - _thisCam.x;
			var ry : Number = _3dpoint.y - _thisCam.y;
			var rz : Number = _3dpoint.z - _thisCam.z;
			
			//	var tmp_rx = this.rx;
			//	this.rx = int(Math.cos(tmp_angle)*tmp_rx-Math.sin(tmp_angle)*this.rz);
			//	this.rz = int(Math.sin(tmp_angle)*tmp_rx+Math.cos(tmp_angle)*this.rz);
			
			var tmp_rx : Number = rx;
			rx = _thisCam.cos_y * tmp_rx - _thisCam.sin_y * rz;
			rz = _thisCam.sin_y * tmp_rx + _thisCam.cos_y * rz;
			
			var tmp_rz : Number = rz;
			rz = _thisCam.cos_x * tmp_rz - _thisCam.sin_x * ry;
			ry = _thisCam.sin_x * tmp_rz + _thisCam.cos_x * ry;
			
			_3dpoint.rx = int(rx);
			_3dpoint.ry = int(ry);
			_3dpoint.rz = int(rz);
		}
		
	}
}